﻿using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Enums;
using SelfMadeMillionaire.Core.Services;
using SelfMadeMillionaire.Web.Infrastructure.ViewModels.TrackingPortfolio;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace SelfMadeMillionaire.Web.Infrastructure.Controllers.Admin
{
	public class TrackingPortfolioController : AdminController
	{
		ITrackingPortfolioService _trackingPortfolioService;

		public TrackingPortfolioController(ITrackingPortfolioService trackingPortfolioService)
		{
			_trackingPortfolioService = trackingPortfolioService;
		}

		private ActionResult Index(TrackingPortfolioType trackingPortfolioType)
		{
            var info = _trackingPortfolioService.Get(trackingPortfolioType);
            var model = new TrackingPortfolioViewModel
			{
				TrackingPortfolio = info.TrackingPortfolio,
				DowIndustrials = info.DowIndustrials,
				SP500 = info.SP500,
				Nasdaq = info.Nasdaq,

                TrackingPortfolioReturn = info.TrackingPortfolioReturn,
                DowIndustrialsReturn = info.DowIndustrialsReturn,
                SP500Return = info.SP500Return,
                NasdaqReturn = info.NasdaqReturn,
                
				ToDate = info.ToDate,
				FromDate = info.FromDate,
				TPtype = trackingPortfolioType
			};

            if (model.FromDate.Year == 1)
                model.FromDate = DateTime.Today.Date;
            if (model.ToDate.Year == 1)
                model.ToDate = DateTime.Today.Date;

			ViewBag.Title = GetTitle(trackingPortfolioType);

			return View("Index", model);
		}

		[HttpPost]
		public ActionResult Index(TrackingPortfolioViewModel model)
		{
			if (ModelState.IsValid)
			{
				_trackingPortfolioService.Save(new TrackingPortfolioInfo
				{
					TrackingPortfolio = model.TrackingPortfolio,
					DowIndustrials = model.DowIndustrials,
					SP500 = model.SP500,
					Nasdaq = model.Nasdaq,

                    TrackingPortfolioReturn = model.TrackingPortfolioReturn,
                    DowIndustrialsReturn = model.DowIndustrialsReturn,
                    SP500Return = model.SP500Return,
                    NasdaqReturn = model.NasdaqReturn,

					ToDate = model.ToDate,
					FromDate = model.FromDate,
					TPType = (byte)model.TPtype
				});

			}

			ViewBag.Title = GetTitle(model.TPtype);

			return View("Index", model);
		}

		private string GetTitle(TrackingPortfolioType tpType)
		{
            if (tpType == TrackingPortfolioType.SMM)
                return "SMM Tracking Portfolio";
            else if (tpType == TrackingPortfolioType.MMM)
                return "MMM Tracking Portfolio";
            else if (tpType == TrackingPortfolioType.Beginners)
                return "Beginners 101 Tracking Portfolio";

            return "SMM Tracking Portfolio";
		}

		[HttpGet]
		public ActionResult SMM()
		{
			return Index(TrackingPortfolioType.SMM);
		}

		[HttpGet]
		public ActionResult MMM()
		{
			return Index(TrackingPortfolioType.MMM);
		}

        [HttpGet]
        public ActionResult Beginners101()
        {
            return Index(TrackingPortfolioType.Beginners);
        }
	}
}
